package com.kll.tv.views

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.kll.tv.R
import com.kll.tv.views.adapter.ViewPagerAdapter
import com.kll.tv.views.fragment.ImageReviewFragment


/**
 * 图片预览Activity
 */
class ImageReviewActivity: AppCompatActivity() {
    private var fragments: MutableList<Fragment> = mutableListOf()
    private var currentIndex = 0
    private lateinit var viewPager: ViewPager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_review)
        init()
    }

    private fun init() {
        viewPager = findViewById(R.id.vp_content)

        var images = intent.getBundleExtra("data")!!.getSerializable("data") as ArrayList<String>
        for(image in images) {

            val fragment = ImageReviewFragment()
            val bundle = Bundle()
            bundle.putString("imageUrl", image)
            fragment.arguments = bundle

            fragments.add(fragment)
        }

        currentIndex = intent.getIntExtra("index", 0)

        var viewPagerAdapter = ViewPagerAdapter(supportFragmentManager, fragments)

        viewPager.adapter = viewPagerAdapter
        viewPager.setOffscreenPageLimit(2)
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {}
            override fun onPageSelected(i: Int) {
                switchPager(i)
            }

            override fun onPageScrollStateChanged(i: Int) {}
        })

        viewPager.currentItem = currentIndex
    }

    /**
     * 页面切换
     */
    private fun switchPager(currentPage: Int) {

    }

}